require('dotenv').config();
const mongoose = require('mongoose');
const User = require('./models/User');

// 测试用户数据
const testUsers = [
  {
    username: 'zhangxiaoming',
    email: 'zhangxiaoming@example.com',
    password: '123456',
    name: '张小明',
    bio: '我是一个旅行爱好者',
    location: {
      city: '雅典',
      country: '希腊',
      coordinates: [23.7275, 37.9838]
    }
  },
  {
    username: 'testuser',
    email: 'test@example.com',
    password: '123456',
    name: 'Test User',
    bio: '这是一个测试用户',
    location: {
      city: '圣托里尼',
      country: '希腊',
      coordinates: [25.4615, 36.3932]
    }
  },
  {
    username: 'admin',
    email: 'admin@example.com',
    password: 'admin123',
    name: 'Administrator',
    bio: '系统管理员',
    isVerified: true,
    location: {
      city: '塞萨洛尼基',
      country: '希腊',
      coordinates: [22.9444, 40.6401]
    }
  },
  {
    username: 'traveler',
    email: 'user@travel.com',
    password: 'travel123',
    name: 'Travel User',
    bio: '热爱旅行的用户',
    location: {
      city: '米科诺斯',
      country: '希腊',
      coordinates: [25.3667, 37.4467]
    }
  }
];

async function initTestData() {
  try {
    // 连接数据库
    await mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost:27017/traveller', {
      useNewUrlParser: true,
      useUnifiedTopology: true
    });

    console.log('✅ 数据库连接成功');

    // 删除现有用户（如果需要）
    await User.deleteMany({});
    console.log('🗑️ 清除现有用户数据');

    // 创建测试用户
    const createdUsers = [];
    
    for (const userData of testUsers) {
      try {
        const user = new User(userData);
        await user.save();
        createdUsers.push(user);
        console.log(`✅ 创建用户: ${user.email}`);
      } catch (error) {
        console.error(`❌ 创建用户失败 ${userData.email}:`, error.message);
      }
    }

    console.log(`\n🎉 初始化完成！创建了 ${createdUsers.length} 个测试用户`);
    console.log('\n📋 测试账户列表:');
    
    createdUsers.forEach(user => {
      console.log(`   📧 邮箱: ${user.email}`);
      console.log(`   👤 用户名: ${user.username}`);
      console.log(`   🔑 密码: 请查看源代码中的testUsers数组`);
      console.log(`   📍 位置: ${user.location.city}, ${user.location.country}`);
      console.log('   ---');
    });

  } catch (error) {
    console.error('❌ 初始化失败:', error);
  } finally {
    await mongoose.connection.close();
    console.log('👋 数据库连接已关闭');
    process.exit(0);
  }
}

// 运行初始化
if (require.main === module) {
  initTestData();
}

module.exports = { initTestData }; 